package com.qf.elasticsearch.query.impl;

import com.qf.elasticsearch.query.QueryMatchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * create by 郑大仙丶
 * 2019/11/21 19:43
 */
@Service
public class QueryMatchServiceImpl implements QueryMatchService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public List<Map<String, Object>> matchAll(String index, String type) throws IOException {
        //1. 创建查询request
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        //2. 设置查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());
        request.source(builder);
        //3. 执行查询
        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        //4. 封装结果
        SearchHit[] hits = resp.getHits().getHits();
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : hits) {
            result.add(hit.getSourceAsMap());
        }
        //5. 返回
        return result;
    }

    @Override
    public List<Map<String, Object>> match(String index, String type, String name, String value) throws IOException {
        //1. 创建查询request
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        //2. 设置查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery(name,value));
        request.source(builder);
        //3. 执行查询
        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        //4. 封装结果
        SearchHit[] hits = resp.getHits().getHits();
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : hits) {
            result.add(hit.getSourceAsMap());
        }
        //5. 返回
        return result;
    }

    @Override
    public List<Map<String, Object>> matchMulti(String index, String type, String value, String[] fields) throws IOException {
        //1. 创建查询request
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        //2. 设置查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.multiMatchQuery(value,fields));
        request.source(builder);
        //3. 执行查询
        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        //4. 封装结果
        SearchHit[] hits = resp.getHits().getHits();
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : hits) {
            result.add(hit.getSourceAsMap());
        }
        //5. 返回
        return result;
    }

    @Override
    public List<Map<String, Object>> matchPhrase(String index, String type, String name, String value) throws IOException {
        //1. 创建查询request
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        //2. 设置查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchPhraseQuery(name,value));
        request.source(builder);
        //3. 执行查询
        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        //4. 封装结果
        SearchHit[] hits = resp.getHits().getHits();
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : hits) {
            result.add(hit.getSourceAsMap());
        }
        //5. 返回
        return result;
    }


}
